package com.example.surveyapplication;

import java.util.ArrayList;
import java.util.List;

import object.TestObject;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class TestListViewAdapter extends ArrayAdapter<TestObject> implements OnClickListener {

	private ArrayList<TestObject> objects;
	private Context context;

	public TestListViewAdapter(Context context, List<TestObject> objects) {
		super(context, R.layout.item_test_listview, objects);
		// TODO Auto-generated constructor stub
		this.objects = (ArrayList<TestObject>) objects;
		this.context = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		if (view == null)
			view = new ItemTestListView(context);

		final TestObject testObject = objects.get(position);
		if (testObject != null) {
			TextView tvgetDisplayName = ((ItemTestListView) view).tvQuestion;
			RadioButton radioButton1 = ((ItemTestListView) view).radioButton1;
			RadioButton radioButton2 = ((ItemTestListView) view).radioButton2;
			RadioButton radioButton3 = ((ItemTestListView) view).radioButton3;
			RadioButton radioButton4 = ((ItemTestListView) view).radioButton4;
			RadioButton radioButton5 = ((ItemTestListView) view).radioButton5;
			RadioGroup radioGroup = ((ItemTestListView) view).radioGroup;

			tvgetDisplayName.setText(testObject.getQuestion());
			switch (testObject.getNoAnswer()) {
			case 0:
				radioButton1.setVisibility(View.INVISIBLE);
				radioButton2.setVisibility(View.INVISIBLE);
				radioButton3.setVisibility(View.INVISIBLE);
				radioButton4.setVisibility(View.INVISIBLE);
				radioButton5.setVisibility(View.INVISIBLE);
				break;
			case 1:
				radioButton1.setVisibility(View.INVISIBLE);
				radioButton2.setVisibility(View.INVISIBLE);
				radioButton3.setVisibility(View.INVISIBLE);
				radioButton4.setVisibility(View.INVISIBLE);
				break;
			case 2:
				radioButton1.setVisibility(View.INVISIBLE);
				radioButton2.setVisibility(View.INVISIBLE);
				radioButton3.setVisibility(View.INVISIBLE);
				break;
			case 3:
				radioButton1.setVisibility(View.INVISIBLE);
				radioButton2.setVisibility(View.INVISIBLE);
				break;
			case 4:
				radioButton1.setVisibility(View.INVISIBLE);
				break;
			case 5:
				break;
			default:
				
				break;
			}

			radioGroup.clearCheck();
			radioButton1.setChecked(testObject.getAnswers().get(0));
			radioButton2.setChecked(testObject.getAnswers().get(1));
			radioButton3.setChecked(testObject.getAnswers().get(2));
			radioButton4.setChecked(testObject.getAnswers().get(3));
			radioButton5.setChecked(testObject.getAnswers().get(4));
			
			radioButton1.setTag(position);
			radioButton2.setTag(position);
			radioButton3.setTag(position);
			radioButton4.setTag(position);
			radioButton5.setTag(position);

			radioButton1.setOnClickListener(this);
			radioButton2.setOnClickListener(this);
			radioButton3.setOnClickListener(this);
			radioButton4.setOnClickListener(this);
			radioButton5.setOnClickListener(this);
		}

		return view;
	}
	
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.radioButton1:
			objects.get(Integer.parseInt(view.getTag().toString())).setAnswerIndex(0);
			break;
		case R.id.radioButton2:
			objects.get(Integer.parseInt(view.getTag().toString())).setAnswerIndex(1);
			break;
		case R.id.radioButton3:
			objects.get(Integer.parseInt(view.getTag().toString())).setAnswerIndex(2);
			break;
		case R.id.radioButton4:
			objects.get(Integer.parseInt(view.getTag().toString())).setAnswerIndex(3);
			break;
		case R.id.radioButton5:
			objects.get(Integer.parseInt(view.getTag().toString())).setAnswerIndex(4);
			break;
		default:
			break;
		}
	}

	private class ItemTestListView extends LinearLayout {

		TextView tvQuestion;
		RadioButton radioButton1;
		RadioButton radioButton2;
		RadioButton radioButton3;
		RadioButton radioButton4;
		RadioButton radioButton5;
		RadioGroup radioGroup;

		public ItemTestListView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			LayoutInflater listItem = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			listItem.inflate(R.layout.item_test_listview, this, true);

			this.tvQuestion = (TextView) findViewById(R.id.tvQuestion);
			radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
			radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
			radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
			radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
			radioButton5 = (RadioButton) findViewById(R.id.radioButton5);
			radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
		}
	}

}
